package controllers {
  import com.axon.base.helpers.AxFailure;
  import com.axon.base.managers.AxErrorManager;
  import com.axon.base.managers.AxPopUpManager;
  import com.axon.dataservices.helpers.AxDSUtils;
  import com.axon.mvc.helpers.AxVerbs;
  import com.axon.mvc.patterns.command.AxSimpleCommand;

  import flash.net.URLRequest;
  import flash.net.navigateToURL;

  import helpers.Verbs;

  import models.proxies.DocumentoApresentadoProxy;
  import models.proxies.PleitoProxy;
  import models.vo.DocumentoApresentado;
  import models.vo.Pleito;
  import models.vo.User;

  import org.restfulx.Rx;

  import views.components.PleitoPopUpAceitacaoView;
  import views.components.PleitoPopUpRegDoctosView;
  import views.components.PleitoPopUpRegProtocoloView;
  import views.components.PleitoPopUpRejeicaoView;
  import views.mediators.PleitoPopUpAceitacaoMediator;
  import views.mediators.PleitoPopUpApreciacaoMediator;
  import views.mediators.PleitoPopUpDistribuicaoMediator;
  import views.mediators.PleitoPopUpEdicaoDataLaudoMediator;
  import views.mediators.PleitoPopUpRegDoctosMediator;
  import views.mediators.PleitoPopUpRegProtocoloMediator;
  import views.mediators.PleitoPopUpRejeicaoMediator;
  import views.mediators.PleitoPropMediator;

  public class PleitoCommand extends AxSimpleCommand {
    public var dlgProtocoloView:PleitoPopUpRegProtocoloView;

    public var regDoctosView:PleitoPopUpRegDoctosView;

    public var dlgAceiteView:PleitoPopUpAceitacaoView;

    public var dlgRejeicaoView:PleitoPopUpRejeicaoView;

    private var pleito:Pleito;

    private var pleitoProxy:PleitoProxy;

    private var documentoApresentadoProxy:DocumentoApresentadoProxy;

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execList(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.list(body as String, extraInfo);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execReadPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPropMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, false));

      AppFacade(facade).callController(Pleito, AxVerbs.READ, body);
    }

    public function execRead(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.read(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execAnalisarPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPropMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, true));

      AppFacade(facade).callController(Pleito, AxVerbs.READ, body);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execRegistrarProtocoloPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpRegProtocoloMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito));
    }

    public function execRegistrarProtocolo(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.registrarProtocolo(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execRegistrarDoctosPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpRegDoctosMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito));

      AppFacade(facade).callController(DocumentoApresentado, AxVerbs.LIST, body);
    }

    public function execRegistrarDoctosAdic(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.registrarDoctosAdic(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execAceitarInscricaoPopUp(sender:Object, body:Object, extraInfo:Object):void {
      var pleito:Pleito = body as Pleito;
      if (pleito.algumDocumentoComBronca()) {
        AxErrorManager.showFailure(new AxFailure("Pleito com documento(s) obrigatório(s) não entruegue(s) e/ou documento(s) vencido(s). Portanto, não pode ser aceito."));
        return;
      }
      AppFacade(facade).regMediator(
        new PleitoPopUpAceitacaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito));
    }

    public function execAceitarInscricao(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.aceitarInscricao(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execRejeitarInscricaoPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpRejeicaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito));
    }

    public function execRejeitarInscricao(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.rejeitarInscricao(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execDistribuirPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpDistribuicaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito));

      AppFacade(facade).callController(User, Verbs.LIST_ANALISTAS);
    }

    public function execDistribuir(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.distribuir(body as Pleito);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execAvancarPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpApreciacaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, Verbs.AVANCAR));
    }

    public function execAvancar(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.avancar(body as Pleito, extraInfo);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execVoltarParaAnalisePopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpApreciacaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, Verbs.VOLTAR_PARA_ANALISE));
    }

    public function execVoltarParaAnalise(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.voltarParaAnalise(body as Pleito, extraInfo);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execArquivarPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpApreciacaoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, Verbs.ARQUIVAR));
    }

    public function execArquivar(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.arquivar(body as Pleito, extraInfo.texto);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execEditarDataLaudoPopUp(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).regMediator(
        new PleitoPopUpEdicaoDataLaudoMediator((facade as AppFacade).loungeMediator.displayObject,
        AxDSUtils.clone(body) as Pleito, extraInfo as String));
    }

    public function execEditarDataLaudo(sender:Object, body:Object, extraInfo:Object):void {
      AppFacade(facade).pleitoProxy.editarDataLaudo(body as Pleito, extraInfo);
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function execGenerateReport(sender:Object, body:Object, extraInfo:Object):void {
      var serverAction:String;
      var url:String;
      var request:URLRequest;
//            var variables		: URLVariables = new URLVariables();
//            
//            variables._session_id = Rx.sessionToken;

      switch (extraInfo as String) {

        case "relatRelVistoria":
          serverAction = "vistoria";
          break;
        case "relatRelAnalise":
          serverAction = "analise";
          break;
        case "relatParecerAnalise":
          serverAction = "analise";
          break;
        case "relatFluxoTramitacao":
          serverAction = "deliberacao";
          break;
        case "relatPortariaMinuta":
          serverAction = "portaria?minuta=true";
          break;
        case "relatPortariaVersaoFinal":
          serverAction = "portaria";
          break;
        case "relatLaudoMinuta":
          serverAction = "laudo?minuta=true";
          break;
        case "relatLaudoVersaoFinal":
          serverAction = "laudo";
          break;
        case "relatOficioEmpresa":
          serverAction = "oficio?tipo=empresa";
          break;
        case "relatOficioEmpresaLiberacao":
          serverAction = "oficio?tipo=empresa&fase=liberacao";
          break;
        case "relatOficioReceita":
          serverAction = "oficio";
          break;
        case "relatOficioBnb":
          serverAction = "oficio?tipo=bnb";
          break;
        case "relatOficioMarinha":
          serverAction = "oficio?tipo=marinha";
          break;
      }

      url = Rx.httpRootUrl + "pleitos/" + Pleito(body).id + "/" + serverAction;
      request = new URLRequest(url);
//            request.data	= variables;

      try {

        navigateToURL(request);
      }

      catch (e:Error) {

        AxPopUpManager.messageDlg("Erro na geração do relatório: " + e.message, "Erro");
      }
    }
  }
}